package org.linlinjava.litemall.wx.web;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.linlinjava.litemall.core.util.ResponseUtil;
import org.linlinjava.litemall.core.validator.Sort;
import org.linlinjava.litemall.db.domain.PiccNurse;
import org.linlinjava.litemall.db.service.PiccNurseService;
import org.linlinjava.litemall.wx.service.WxPiccNurseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("wx/nurse")
@Validated
public class WxPiccNurseController {
    private final Log logger = LogFactory.getLog(WxPiccNurseController.class);
    @Autowired
    PiccNurseService piccNurseService;

    @Autowired
    WxPiccNurseService wxPiccNurseService;

    /**
     * 查询所有的护士信息list
     * @param page
     * @param limit
     * @param sort
     * @param order
     * @return
     */
    @GetMapping("list")
    public Object getPatients(@RequestParam(defaultValue = "1") Integer page,
                              @RequestParam(defaultValue = "10") Integer limit,
                              @Sort @RequestParam(defaultValue = "id") String sort,
                              @RequestParam(defaultValue = "desc") String order){
        logger.debug("enter list");
        List<PiccNurse> mList = piccNurseService.getNurseList(page, limit, sort, order);
        logger.debug(mList);
        return ResponseUtil.ok(mList);
    }

    /**
     * 返回一个护士的所有字段，根据id查询
     * @param id
     * @return
     */
    @GetMapping("detail")
    public Object getDetail(@RequestParam Integer id){
        PiccNurse p = piccNurseService.getDetailById(id);
        return ResponseUtil.ok(p);
    }

    /**
     * 添加或更新护士
     * @param record
     * @return
     */
    @PostMapping("create")
    public Object add(@RequestBody PiccNurse record){
        logger.debug("enter nurse create");
        PiccNurse p = wxPiccNurseService.add(record);
        return p!=null ? ResponseUtil.ok(p) : ResponseUtil.fail(402, "nurse not exist");
    }
}
